package scuts.core;

import scuts.core.types.Option;
import scuts.macro.Print;
using scuts.core.extensions.Options;

#if macro
import haxe.macro.Expr.ExprRequire;
import haxe.macro.Expr.Expr;
import scuts.macro.Parse;
#end


class GetOrElseOption {
  
  /**
   * @param o An instance of Option
   * @param elseExpr expression of type T that should be used if o is None
   * @return the value of type T which is wrapped in o or elseExpr
   */
  @:macro public static function getOrElse <T>(o:ExprRequire<Option<T>>, elseExpr:ExprRequire<T>):ExprRequire<T>
  {
    return Parse.parse("if (scuts.core.extensions.Options.isSome(o)) scuts.core.extensions.Options.value(o) else $0", [elseExpr], o.pos);
  }
  
  public static function getOrElseNoMacro <T>(o:Option<T>, elseValue:T):T
  {
    return if (o.isSome()) o.value() else elseValue;
  }
}

class GetOrElseDynamic
{
  
  @:macro public static function getOrElse <T>(o:ExprRequire<T>, elseExpr:ExprRequire<T>):ExprRequire<T>
  {
    return Parse.parse("if (o != null) o else $0", [elseExpr], o.pos);
  
     
  }
  
  public static function getOrElseNoMacro <T>(o:T, elseValue:T):T
  {
    return if (o == null) elseValue else o;
  }
  
  
}
